Skip to content

Fix hang when a ZIM creation is abandoned before finishing - #378

Merged
benoit74 merged 1 commit into
openzim:mainfrom
pawank925:issue-300-abandoned-creator-hang
Nov 5, 2026
Merged

benoit74 merged 1 commit into
openzim:mainfrom
pawank925:issue-300-abandoned-creator-hang

Conversation

@pawank925

@pawank925 pawank925 commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #300

The duplicate RuntimeError is expected in the reported program (duplicate_ok defaults to False). The hang happens afterwards, when the interpreter exits with the Creator still unfinished.

libzim has no cancel API and its worker threads call back into Python (ContentProviderWrapper.feed()) to read item content. Sampling the hung process shows:

  • main thread: subtype_dealloc → libzim Creator::~Creator() → CreatorData::~CreatorData() → quitAllThreads() → std::thread::join(), holding the GIL;
  • worker thread: Cluster::close() → content feed → PyGILState_Ensure() → take_gil(), waiting for the GIL.

Classic join/GIL deadlock. It needs pending cluster data to reproduce, which is why it only shows up once items fill a cluster. The libzim-only script from the issue did not hang because it used with creator:, whose __exit__ calls finishZimCreation() with the GIL released.

Fix

finishZimCreation() is the only way to stop libzim's threads, and libzim's binding releases the GIL around it while worker threads drain. When a creation is abandoned (can_finish is False), zim.Creator now:

  • runs that finalization with the GIL released,
  • then removes the resulting ZIM (the workaround's intent is still "no unwanted ZIM on disk").

This happens on explicit finish() after an error, on garbage collection, and through an atexit sweep for creators that are neither finished nor collected before interpreter exit (the exact case in the report). The old docstring advice about segfaults/leaving the temp folder can be dropped.

Normal finish and workaround_nocancel=False behaviour are unchanged: the ZIM is still produced when the workaround is disabled, so the "delete the in-progress ZIM" decision discussed in #300 stays opt-out.

Tests

  • test_duplicate_on_filled_cluster_terminates runs the reported program in a subprocess and asserts it exits (and leaves no ZIM). It times out without the fix.
  • test_duplicate_error_discards_zim, test_garbage_collected_creation_discards_zim and test_atexit_sweep_discards_unfinished_creation cover the explicit-finish, GC and atexit paths.
  • test_without_workaround_keeps_zim_on_duplicate covers workaround_nocancel=False: the ZIM is still built despite the duplicate errors.

Full tests/zim suite plus the rest of the suite (1104 passed, 69 skipped here), ruff, ruff format and pyright pass, and zim/creator.py is back to 100% line and branch coverage. The only failures locally are the pre-existing tests/image ones, which need gifsicle and friends, absent in this environment.

@codecov

codecov Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (5f93f6b) to head (fcc6750).

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #378   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           42        42           
  Lines         2867      2900   +33     
  Branches       432       438    +6     
=========================================
+ Hits          2867      2900   +33     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@rgaudin rgaudin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't tested but that looks great; thanks. Was a pain.

@benoit74

benoit74 commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator

@pawank925 is this ready for full review? Any reason to not link this PR to the issue so it get closed when PR is merged?

libzim cannot cancel a creation and its worker threads call back into
Python to read item content. Abandoning a Creator before finishing left
those threads waiting for the GIL while the interpreter held it and
joined them from the libzim destructor, deadlocking the process exit.

Finish the creation with the GIL released so the workers can drain, then
remove the resulting ZIM. This now happens when finishing after an error
and, through an atexit sweep, when the creator is neither finished nor
collected before the interpreter exits.

Closes openzim#300
@pawank925
pawank925 force-pushed the issue-300-abandoned-creator-hang branch from 9c6cf43 to fcc6750 Compare November 5, 2026 11:52
@pawank925

pawank925 commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks @benoit74. Yes, this is ready for a full review — the fix, the CHANGELOG entry and the regression tests are all in.

I've cleared the loose ends while you were asking:

  • Issue link: no reason not to, the description just never mentioned it. Added Fixes #300, so Program hangs forever when adding two items at same path #300 closes on merge now.
  • Merge conflict: Patch vendored wombat.js to convert POST to GET in fetch() #379 added a ### Fixed line at the same place in CHANGELOG.md, so that was the only conflict — no code involved. Rebased on main; the PR is mergeable again.
  • Codecov patch coverage: the 3 uncovered lines were the atexit sweep (Creator._abandon_live_creators); it only ran inside the subprocess reproducer, which isn't measured. Added test_atexit_sweep_discards_unfinished_creation, which calls the same callable atexit does, plus test_without_workaround_keeps_zim_on_duplicate for the two workaround_nocancel=False branches. zim/creator.py is back to 100% line and branch coverage locally.

On the point from #300 about deleting an in-progress ZIM being risky: that removal stays behind workaround_nocancel, as @rgaudin noted, so a scraper that wants libzim's partial ZIM still gets it — the new test pins that behaviour down.

@pawank925
pawank925 requested a review from rgaudin November 5, 2026 12:27
@pawank925

pawank925 commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor Author

@benoit74 once look into this please whenever you can thanks!

@benoit74 benoit74 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you!

@benoit74
benoit74 merged commit 8cce64e into openzim:main Nov 5, 2026
10 checks passed
@benoit74 benoit74 mentioned this pull request Nov 6, 2026
18 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Program hangs forever when adding two items at same path

3 participants